

#include "enemy.h"
#include "bullet.h"
#include "animation.h"
#include "gameplayscene.h"
#include "resourcemanager.h"
#include "CCSkeletalSprite.h"

#define __super				Unit

Enemy::Enemy()
{

}

Enemy::~Enemy()
{

}

bool Enemy::update( float ticks )
{
	sprite->update(ticks);
	updateBullets(ticks);

	switch (state)
	{
	case UnitState::birth:
		handleStateBirth(ticks);
		break;
	case UnitState::walk_to_flag:
		handleStateWalk(ticks);
		break;
	case UnitState::stand:
		handleStateStand(ticks);
		break;
	case UnitState::melee:
		handleStateMelee(ticks);
		break;
	case UnitState::shoot:
		handleStateShoot(ticks);
		break;
	case UnitState::cast:
		handleStateCast(ticks);
		break;
	case UnitState::reload:
		handleStateReload(ticks);
		break;
	case UnitState::dying:
		handleStateDying(ticks);
		break;
	default:
		break;
	}

	return true;
}

void Enemy::handleStateBirth( float ticks )
{
	if(sprite && sprite->animationIsDone()) {
		transformState(UnitState::walk_to_flag);
	}
}

void Enemy::handleStateWalk( float ticks )
{
	if(!move(ticks)) {
		nextWaypointIndex++;
		if(nextWaypointIndex >= waypoints.size()) {
			transformState(UnitState::escaped);
		} else {
			moveTo(waypoints[nextWaypointIndex]);
		}
	}
}

void Enemy::handleStateStand( float ticks )
{
	if(!target) {
		transformState(UnitState::walk_to_flag);
	}
}

void Enemy::handleStateMelee( float ticks )
{
	if(sprite->animationIsDone()) {
		if(target) {
			float distance = position.getDistance(target->position);
			if(distance <= 30.0f) {
				transformState(UnitState::reload);
			} else {
				target = nullptr;
				transformState(UnitState::walk_to_flag);
			}
		} else {
			target = nullptr;
			transformState(UnitState::walk_to_flag);
		}
	}
}

void Enemy::handleStateShoot( float ticks )
{

}

void Enemy::handleStateCast( float ticks )
{

}

void Enemy::handleStateReload( float ticks )
{
	_ticks += ticks;
	if(_ticks >= attribute.interval) {
		_ticks = 0.0f;
		transformState(previousState);
	}
}

void Enemy::handleStateDying( float ticks )
{
	if(sprite->animationIsDone()) {
		transformState(UnitState::died);
	}
}

void Enemy::attack()
{
	if(target) {
		Bullet* bullet = new Bullet();
		bullet->unitID = id;
		bullet->meleeDamage = attribute.meleeDamage;
		bullet->init();
		target->bullets.push_back(bullet);
	}

}

void Enemy::shoot()
{
}

void Enemy::cast()
{

}

void Enemy::summon()
{

}

void Enemy::handleBulletHit( Bullet* bullet, float damageFactor )
{
	__super::handleBulletHit(bullet, damageFactor);

	if(state == UnitState::stand) {
		if(target && bullet->unitID == target->id) {
			if(target->position.x > position.x) {
				sprite->setFlipX(false);
			} else {
				sprite->setFlipX(true);
			}

			transformState(UnitState::melee);
		}
	}
}

void Enemy::onAttack( EventData* event )
{
	if(strcmp(event->name, "shoot") == 0) {
		shoot();
	} else if(strcmp(event->name, "melee") == 0) {
		attack();
	} else if(strcmp(event->name, "cast") == 0) {
		cast();
	}

}
